//
//  NetWorkEngin.m
//  BiYeDaBian
//
//  Created by TonyGenius on 16/1/14.
//  Copyright © 2016年 GSH. All rights reserved.
//

#import "NetWorkEngin.h"
#import "AFNetworking/AFNetworking.h"

@implementation NetWorkEngin


/**
 *
 *POST 请求
 * @1 返回block中是字典
 * @2 返回block中时数据类型
 */
+(void)netWorking:(NSDictionary *)dictionary andURl:(NSString *)url andFinishBlock:(void (^)(NSDictionary *, NSError *))block
{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
   [manager POST:url parameters:dictionary progress:^(NSProgress * _Nonnull uploadProgress) {
       
       NSLog(@"%@",uploadProgress);
       
   } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
       NSLog(@"%@",responseObject);
       
       block(responseObject,task.error);
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
       NSLog(@"%@____%@",task.error,error);
       
       block(nil,error);
   }];

}


+ (void)postRequestWithParametes:(NSDictionary *)dictionary andURL:(NSString *)url andFinishBlock:(void (^)(NSDictionary *, NSError *))block
{
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
        [manager POST:url parameters:dictionary progress:^(NSProgress * _Nonnull uploadProgress) {
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject);
    
            block(responseObject, task.error);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败____%@",error);
    
            block(nil,task.error);
        }];
}

/**
 *
 *GET 请求
 * @1返回的block中返回的是字典
 * @2返回的block中返回的是数据类型
 */

+ (void)requestWith:(NSString *)accessToken andURL:(NSString *)url andFinishBlock:(void (^)(NSDictionary *, NSError *))block
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager GET:url parameters:accessToken progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%@",downloadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        block(responseObject,task.error);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
        block(nil,error);
    }];
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    [manager POST:url parameters:accessToken success:^ (AFHTTPRequestOperation *operation , id responObject) {
//       // NSLog(@"%@",responObject);
//        
//        
//    } failure:^(AFHTTPRequestOperation *operation , NSError *error ) {
//       // NSLog(@"%@",operation.error);
//    }];
}

+ (void)getRequestWithParametes:(NSString *)url andBody:(NSDictionary *)dictionary andFinishBlock:(void (^)(NSDictionary *, NSError *))block
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:dictionary progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        
        block(responseObject,task.error);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"请求失败____%@",error);
        block(nil,task.error);
        
    }];
}

@end
